package com.geez.bbl.pdf;

import com.geez.bbl.db.type.BblBook;
import com.geez.bbl.db.type.BblChapter;
import com.geez.bbl.db.type.Builder;
import com.geez.bbl.db.type.Verse;
import com.geez.bbl.util.ProjectResources;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author biniam.gebremichael
 * @since 12/12/11
 */
public class PdfRead {
    /**
     * The resulting PDF file.
     */
    public static final File BODY = ProjectResources.getResourceFile("itext", "columnTigrignaBible.pdf");

    /**
     * Definition of two columns
     */
    public static final float[][] COLUMNS = {
            {36, 36, 296, 806}, {299, 36, 559, 806}
    };


    public void createPdf(List<BblBook> bblBooks, int column)
            throws IOException, DocumentException, SQLException {

        Document document = new Document();
        // step 2
        PdfWriter writer
                = PdfWriter.getInstance(document, new FileOutputStream(BODY));
        // step 3
        document.open();
        ColumnText ct = new ColumnText(writer.getDirectContent());
        for (BblBook book : bblBooks) {

            ct.addText(createMovieInformation(book));
            ct.addText(Chunk.NEWLINE);
        }
        ct.setAlignment(Element.ALIGN_JUSTIFIED);
        ct.setExtraParagraphSpace(6);
        ct.setLeading(0, 1.2f);
        ct.setFollowingIndent(27);
        int linesWritten = 0;
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            ct.setSimpleColumn(
                    COLUMNS[column][0], COLUMNS[column][1],
                    COLUMNS[column][2], COLUMNS[column][3]);
            ct.setYLine(COLUMNS[column][3]);
            status = ct.go();
            linesWritten += ct.getLinesWritten();
          //  column = Math.abs(column - 1);
          if (column == 0)
                document.newPage();
        }

        ct.addText(new Phrase("Lines written: " + linesWritten));
        ct.go();
        // step 5
        document.close();
    }

    public Phrase createMovieInformation(BblBook bblBook) {
        Phrase p = new Phrase();
        p.setFont(Bbl2Pdf.nyala);
        p.add(new Phrase("Title: ", Bbl2Pdf.nyala));
        p.add(bblBook.getLong_name());
        p.add(" ");

        p.add(new Phrase("Original title: ", Bbl2Pdf.nyala));
        p.add(bblBook.getEnglishName());
        p.add(" ");

        p.add(new Phrase("Country: ", Bbl2Pdf.nyala));
        for (BblChapter bblChapter : bblBook.getChapters().values()) {
            for (Verse verse : bblChapter.getVerses()) {
                String num = verse.getStartId() == verse.getEndId() ? String.valueOf(verse.getStartId()) : verse.getStartId() + "-" + verse.getEndId();
                p.add(makeVerse(num, "ABC"));
                p.add(" ");
            }
        }
        p.add(new Chunk("type: ", Bbl2Pdf.nyala));
        p.add(new Chunk(String.valueOf(bblBook.getBook_type()), Bbl2Pdf.nyala));
        p.add(new Chunk(" book No: ", Bbl2Pdf.nyala));
        p.add(new Chunk(String.valueOf(bblBook.getBook_no()), Bbl2Pdf.nyala));
        p.add(new Chunk(" minutes", Bbl2Pdf.nyala));
        p.add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2));
        return p;
    }

    /**
     * Main method.
     *
     * @param args no arguments needed
     * @throws com.itextpdf.text.DocumentException
     * @throws java.io.IOException
     * @throws java.sql.SQLException
     */
    public static void main(String[] args) throws Exception {

        List<BblBook> books = new Builder("ti").build();
        List<BblBook> enBooks = new Builder("en").build();
        PdfRead pdfRead = new PdfRead();
        pdfRead.createPdf(books,0);
        pdfRead.createPdf(enBooks,1);
    }


    private Phrase makeVerse(String number, String value) {
        Phrase cabecalho = new Phrase();
        Chunk fqdi = new Chunk(" " + number + " ", FontFactory.getFont(FontFactory.HELVETICA, 6, Font.ITALIC));
        fqdi.setTextRise(5f);
        cabecalho.add(fqdi);
        cabecalho.add(new Phrase(value, Bbl2Pdf.nyala));
        return cabecalho;
    }
}
